package cn.utils;

import cn.utils.imgs.HttpUtil;
import com.baidu.aip.util.Base64Util;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.*;


public class ImageUtils {
    final static private String API_URL = BaseUtils.getProString("baiduAPIUrl");
    final static private String API_KEY = BaseUtils.getProString("baiduAPIKey");
    final static private String SECRET_KEY = BaseUtils.getProString("baiduAPISecret");
    final static private String TOKEN_URL = BaseUtils.getProString("baiduAccessTokenUrl");
    final static private CloseableHttpClient client = HttpClients.createDefault();
    final static private  ObjectMapper json = new ObjectMapper();
    static private String access_token = "";



    public static String getAccessToken() {
        List<NameValuePair> param = new ArrayList<>();
        param.add(new BasicNameValuePair("grant_type", "client_credentials"));
        param.add(new BasicNameValuePair("client_id", API_KEY));
        param.add(new BasicNameValuePair("client_secret", SECRET_KEY));

        HttpPost post = new HttpPost(TOKEN_URL);
        try {
            post.setEntity(new UrlEncodedFormEntity(param));
            CloseableHttpResponse response = client.execute(post);
            String result = EntityUtils.toString(response.getEntity());

            Map<String, Object> res = json.readValue(result, new TypeReference<Map<String, Object>>() {
            });
            access_token = (String) res.get("access_token");
            LoggerUtils.info("成功获取到百度云AccessToken" + res);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return access_token;
    }

    /**
     * 识别身份证文字
     * @param url 需要识别的图片url
     * @param isFront 是否是正面
     * @return 返回身份证信息
     */

    public static Map<String, Object> getIdCardInfo(String url, boolean isFront, String openid) {
        String side = isFront ? "front" : "back";
        Map<String, Object> res = new HashMap<>();
        try {
            String result = HttpUtil.post(API_URL, getAccessToken(), "id_card_side=" + side + "&detect_risk=true" + "&image=" + url);
            res = json.readValue(result, new TypeReference<Map<String, Object>>() {});
        } catch (Exception e) {
            e.printStackTrace();
        }
        return res;
    }

}
